package com.example.echoewish.ui.main_activity;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.example.echoewish.econstant.Constant;
import com.example.echoewish.econstant.UserInfo;
import com.example.echoewish.repository.UserMessageRepository;
import com.example.echoewish.util.LogUtil;

import java.io.File;
import java.io.IOException;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MyViewModel extends ViewModel {
    MutableLiveData<String> responseMutableLiveData;

    public MyViewModel()
    {
        responseMutableLiveData=new MutableLiveData<>();
    }

    void updateHeadImage(File image)
    {
        UserMessageRepository.getInstance().updateUserHeadImage(UserInfo.getInstance().UserId,image,new UpdateHeadImageCallBack());
    }

    private class UpdateHeadImageCallBack implements Callback<ResponseBody>
    {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            try {
                responseMutableLiveData.postValue(response.body().string());
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            responseMutableLiveData.postValue(Constant.HTTPS_GET_CALLBACK_FAIL);
            new LogUtil().w(LogUtil.CALLBACK_TAG,"更新头像 回调失败"+t);
        }
    }
}
